package com.cloudbroker.bcs.common.emoji;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by xuyn15828 on 2016/4/26.
 */
public class EmojiTrie {
    private EmojiTrie.Node root = new EmojiTrie.Node();

    public EmojiTrie(Collection<Emoji> emojis) {
        Iterator var2 = emojis.iterator();

        while(var2.hasNext()) {
            Emoji emoji = (Emoji)var2.next();
            EmojiTrie.Node tree = this.root;
            char[] var5 = emoji.getUnicode().toCharArray();
            int var6 = var5.length;

            for(int var7 = 0; var7 < var6; ++var7) {
                char c = var5[var7];
                if(!tree.hasChild(c)) {
                    tree.addChild(c);
                }

                tree = tree.getChild(c);
            }

            tree.setEmoji(emoji);
        }

    }

    public EmojiTrie.Matches isEmoji(char[] sequence) {
        if(sequence == null) {
            return EmojiTrie.Matches.POSSIBLY;
        } else {
            EmojiTrie.Node tree = this.root;
            char[] var3 = sequence;
            int var4 = sequence.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                char c = var3[var5];
                if(!tree.hasChild(c)) {
                    return EmojiTrie.Matches.IMPOSSIBLE;
                }

                tree = tree.getChild(c);
            }

            return tree.isEndOfEmoji()? EmojiTrie.Matches.EXACTLY: EmojiTrie.Matches.POSSIBLY;
        }
    }

    public Emoji getEmoji(String unicode) {
        EmojiTrie.Node tree = this.root;
        char[] var3 = unicode.toCharArray();
        int var4 = var3.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            char c = var3[var5];
            if(!tree.hasChild(c)) {
                return null;
            }

            tree = tree.getChild(c);
        }

        return tree.getEmoji();
    }

    private class Node {
        private Map<Character, Node> children;
        private Emoji emoji;

        private Node() {
            this.children = new HashMap();
        }

        private void setEmoji(Emoji emoji) {
            this.emoji = emoji;
        }

        private Emoji getEmoji() {
            return this.emoji;
        }

        private boolean hasChild(char child) {
            return this.children.containsKey(Character.valueOf(child));
        }

        private void addChild(char child) {
            this.children.put(Character.valueOf(child), EmojiTrie.this.new Node());
        }

        private EmojiTrie.Node getChild(char child) {
            return (EmojiTrie.Node)this.children.get(Character.valueOf(child));
        }

        private boolean isEndOfEmoji() {
            return this.emoji != null;
        }
    }

    static enum Matches {
        EXACTLY,
        POSSIBLY,
        IMPOSSIBLE;

        private Matches() {
        }

        public boolean exactMatch() {
            return this == EXACTLY;
        }

        public boolean impossibleMatch() {
            return this == IMPOSSIBLE;
        }
    }
}
